<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\QyLevelJiang;
use app\common\model\Shang;
use \think\Request;
use think\facade\View;
use app\common\model\QyLevel as QyLevelModel;
use app\common\model\GoodsList;

class QyLevel extends Base
{
    /**
     * 等级列表
     */
    public function index(Request $request)
    {
        $keyword = trim(input('get.keyword'));
        if (!empty($keyword)) {
            $whe[] = ['title', 'like', '%' . $keyword . '%'];
        }
//        $type = input('get.type');
//        if (!empty($type)) {
//            $whe[] = ['type', '=', $type];
//        }
        $whe[] = ['id', '>', 0];
        $field = "*";
        $order = "level asc";
        $data = QyLevelModel::getList($whe, $field, $order, $this->page);
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        return View::fetch("QyLevel/index");

    }

    /**
     * 添加商品
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) {

            return View::fetch("QyLevel/add");
        } else {
            $data = input('post.');
            if (intval($data['number']) <= 0) {
                return $this->renderError("等级不能为空并且不能小于等于0");
            }
            if (!$data['title']) {
                return $this->renderError("名称不能为空");
            }
            if (intval($data['number']) <= 0) {
                return $this->renderError("欧气值不能小于等于0");
            }
            $data['addtime'] = time();
            $data['updatetime'] = time();
            $dd = QyLevelModel::insertGetId($data);

            if ($dd) {
                return $this->renderSuccess("添加成功");
            } else {
                return $this->renderError("网络繁忙，请稍后");
            }
        }
    }

    /**
     * 编辑商品
     */
    public function edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param('id');
            $goods = QyLevelModel::getInfo(['id' => $id], '*');
            View::assign('data', $goods);
            return View::fetch("QyLevel/edit");
        } else {
            $data = input('post.');
            if (intval($data['number']) <= 0) {
                return $this->renderError("等级不能为空并且不能小于等于0");
            }
            if (!$data['title']) {
                return $this->renderError("名称不能为空");
            }
            if (intval($data['number']) <= 0) {
                return $this->renderError("欧气值不能小于等于0");
            }
            $data['updatetime'] = time();
            QyLevelModel::startTrans();
            $id = $data['id'];
            unset($data['id']);
            $dd = QyLevelModel::where(['id' => $id])->update($data);

            if ($dd) {
                QyLevelModel::commit();
                return $this->renderSuccess("编辑成功");
            } else {
                QyLevelModel::rollback();
                return $this->renderError("网络繁忙，请稍后");
            }

        }
    }

    //上、下架商品
    public function cao(Request $request)
    {
        $id = $request->post('id/d');
        $status = $request->post('type/d');
//		if(!in_array($status,array(1,2,3))){
//			return $this->renderError("数据错误");
//		}
        $data = QyLevelModel::where(['id' => $id])->whereNull('deltime')->field('id')->find();
        if (!$data) {
            return $this->renderError("数据不存在");
        }
        $result = QyLevelModel::where(['id' => $id])->update(["deltime" => time()]);
        if ($result) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }

    public function qy_level_jiang_list(Request $request)
    {
        $qy_level_id = $request->param('qy_level_id/d', 0);
        $info = QyLevelModel::where(['id' => $qy_level_id])->find();
        if (!$info) {
            return $this->renderError('请求参数错误');
        }

        $title = trim(input('get.keyword'));
        $type = input('get.type');
        $whe = array();
        $whe[] = ['qy_level_id', '=', $qy_level_id];

        if (!empty($title)) {
            $whe[] = ['title', 'like', '%' . $title . '%'];
        }
        if (!empty($type)) {
            $whe[] = ['type', '=', $type];
        }

        $field = "*";
        $order = "id asc";
        $data = QyLevelJiang::getList($whe, $field, $order, 150);

        foreach ($data['list'] as &$value) {


        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('qy_level_id', $qy_level_id);
        View::assign('type', $type);
        return View::fetch("QyLevel/qy_level_jiang_list");

    }

    public function qy_level_jiang_add(Request $request)
    {
        if (!$request->isPost()) {
            $qy_level_id = input('get.qy_level_id');
            $shang = Shang::where('id', 'between', [11, 38])->select()->toArray();
            $coupon = \app\common\model\Coupon::where([['status', '=', 0],['type','=',2]])->select()->toArray();
            View::assign('shang', $shang);
            View::assign('coupon', $coupon);
            View::assign('qy_level_id', $qy_level_id);
            return View::fetch("QyLevel/qy_level_jiang_add");
        } else {
            $data = input('post.');

            if ($data['type'] == 1){
                if (!$data['coupon_id']){
                    return $this->renderError("请选择优惠券");
                }
                if (!$data['z_num'] || intval($data['z_num']) <= 0){
                    return $this->renderError("优惠券赠送数量不能为空或者小于等于0");
                }
                $coupons = \app\common\model\Coupon::where('id',$data['coupon_id'])->find();
                $data['man_price'] = $coupons['man_price'] ? $coupons['man_price'] : 0;
                $data['jian_price'] = $coupons['price'] ? $coupons['price'] : 0;
                $data['effective_day'] = $coupons['effective_day'] ? $coupons['effective_day'] : 0;
                $data['title'] = $coupons['title'];
                $data['z_num'] = $data['z_num'] ? $data['z_num'] : 0;

            }else{
                if (!$data['title']){
                    return $this->renderError("名称不能为空");
                }
                if (!$data['jiang_price'] || intval($data['jiang_price']) <= 0){
                    return $this->renderError("奖品价值不能为空或者小于等于0");
                }
                if (!$data['money'] || intval($data['money']) <= 0){
                    return $this->renderError("奖品兑换价不能为空或者小于等于0");
                }
                if (!$data['sc_money'] || intval($data['sc_money']) <= 0){
                    return $this->renderError("奖品参考价不能为空或者小于等于0");
                }
                if (!$data['probability'] || $data['probability'] <= 0){
                    return $this->renderError("奖品概率不能为空或者小于等于0");
                }
                if (RegMoney($data['probability'])) {
                    return $this->renderError('奖品概率设置错误,最多保留两位小数');
                }
                if (!$data['imgurl']){
                    return $this->renderError("奖品图片不能为空");
                }
                $prize_code = getPrizeCode() . '_' . time();
                $data['prize_code'] = $prize_code;
            }
            $qy_level = \app\common\model\QyLevel::getInfo(['id' => $data['qy_level_id']]);

            $data['qy_level'] = $qy_level['level'];
            $data['jiang_price'] = $data['jiang_price'] ? $data['jiang_price'] : 0;
            $data['money'] = $data['money'] ? $data['money'] : 0;
            $data['sc_money'] = $data['sc_money'] ? $data['sc_money'] : 0;
            $data['imgurl'] = $data['imgurl'] ? $data['imgurl'] : 0;
            $data['addtime'] = time();
            $data['updatetime'] = time();
            $dd = QyLevelJiang::insertGetId($data);

            if ($dd) {
                return $this->renderSuccess("添加成功");
            } else {
                return $this->renderError("网络繁忙，请稍后");
            }
        }
    }

    public function qy_level_jiang_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param('id/d', 0);
            $data = QyLevelJiang::where(['id' => $id])->find();
            $shang = Shang::where('id', 'between', [11, 38])->select()->toArray();
            $coupon = \app\common\model\Coupon::where([['status', '=', 0],['type','=',2]])->select()->toArray();
            View::assign('shang', $shang);
            View::assign('coupon', $coupon);
            View::assign('data', $data);
            return View::fetch("QyLevel/qy_level_jiang_edit");
        } else {
            $data = input('post.');
            $datas = QyLevelJiang::where(['id' => $data['id']])->find();

            if ($datas['type'] == 1){
                if (!$data['coupon_id']){
                    return $this->renderError("请选择优惠券");
                }
                if (!$data['z_num'] || intval($data['z_num']) <= 0){
                    return $this->renderError("优惠券赠送数量不能为空或者小于等于0");
                }
                $coupons = \app\common\model\Coupon::where('id',$data['coupon_id'])->find();
                $data['man_price'] = $coupons['man_price'] ? $coupons['man_price'] : 0;
                $data['jian_price'] = $coupons['price'] ? $coupons['price'] : 0;
                $data['effective_day'] = $coupons['effective_day'] ? $coupons['effective_day'] : 0;
                $data['title'] = $coupons['title'];
                $data['z_num'] = $data['z_num'] ? $data['z_num'] : 0;
                $data['coupon_id'] = $data['coupon_id'] ? $data['coupon_id'] : 0;

            }else{
                if (!$data['title']){
                    return $this->renderError("名称不能为空");
                }
                if (!$data['jiang_price'] || intval($data['jiang_price']) <= 0){
                    return $this->renderError("奖品价值不能为空或者小于等于0");
                }
                if (!$data['money'] || intval($data['money']) <= 0){
                    return $this->renderError("奖品兑换价不能为空或者小于等于0");
                }
                if (!$data['sc_money'] || intval($data['sc_money']) <= 0){
                    return $this->renderError("奖品参考价不能为空或者小于等于0");
                }
                if (!$data['probability'] || $data['probability'] <= 0){
                    return $this->renderError("奖品概率不能为空或者小于等于0");
                }
                if (RegMoney($data['probability'])) {
                    return $this->renderError('奖品概率设置错误,最多保留两位小数');
                }
                if (!$data['imgurl']){
                    return $this->renderError("奖品图片不能为空");
                }
                $prize_code = getPrizeCode() . '_' . time();
                $data['prize_code'] = $prize_code;
            }

            $data['jiang_price'] = $data['jiang_price'] ? $data['jiang_price'] : 0;
            $data['money'] = $data['money'] ? $data['money'] : 0;
            $data['sc_money'] = $data['sc_money'] ? $data['sc_money'] : 0;
            $data['imgurl'] = $data['imgurl'] ? $data['imgurl'] : 0;
            $data['updatetime'] = time();
            $dd = QyLevelJiang::where(['id' => $data['id']])->update($data);

            if ($dd) {
                return $this->renderSuccess("修改成功");
            } else {
                return $this->renderError("网络繁忙，请稍后");
            }
        }
    }

    public function qy_level_jiang_cao(Request $request)
    {
        $id = $request->post('id/d');
        $status = $request->post('type/d');
//		if(!in_array($status,array(1,2,3))){
//			return $this->renderError("数据错误");
//		}
        $data = QyLevelJiang::where(['id' => $id])->whereNull('deltime')->field('id')->find();
        if (!$data) {
            return $this->renderError("数据不存在");
        }
        $result = QyLevelJiang::where(['id' => $id])->update(["deltime" => time()]);
        if ($result) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }

}
